package com.ruoyi.management_controller.userinfo.interceptor;

import org.springframework.context.annotation.Configuration;
import org.springframework.util.ResourceUtils;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @ClassName: WebAppConfig
 * @Description: 过滤器
 * @Author: TanXJ
 * @Date: 2023/1/30 18:05
 */
@Configuration
public class WebAppConfig implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new CustomInterceptor())
                .addPathPatterns("/userinfo/userinfo/updatePwd")
                .excludePathPatterns("/userinfo/userinfo/userLogin");

//                .excludePathPatterns("/")
//                .excludePathPatterns("/static/**");
//                .excludePathPatterns("/index.html");
    }

//    @Override
//    public void addResourceHandlers(ResourceHandlerRegistry registry) {
//        registry.addResourceHandler("/static/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX+"/static/");
//    }


    /**
     * 重写父类提供的跨域请求处理的接口
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        // 添加映射路径
        registry.addMapping("/**")
                .allowedOriginPatterns("*")  // 允许所有来源
                .allowCredentials(true)      // 允许发送身份验证凭据
                .allowedMethods("GET", "POST", "DELETE", "PUT", "OPTIONS", "HEAD")
                .allowedHeaders("*")
                .exposedHeaders("Server", "Content-Length", "Authorization", "Access-Token",
                        "Access-Control-Allow-Origin", "Access-Control-Allow-Credentials");
    }
}
